import io.netty.bootstrap.Bootstrap
import io.netty.channel.nio.NioEventLoopGroup
import io.netty.channel.socket.SocketChannel
import io.netty.channel.socket.nio.NioSocketChannel
import io.netty.channel.{ChannelInitializer, ChannelOption}
import io.netty.handler.codec.string.{StringDecoder, StringEncoder}

/**
  * Created by Administrator on 2017/9/16.
  */
class Client {

    val Host = "127.0.0.1"
    val Port =  4435

    def start: Unit ={

        val group = new NioEventLoopGroup()
        try {
            val boot = new Bootstrap()
            boot.group(group).channel(classOf[NioSocketChannel]).option(ChannelOption.TCP_NODELAY, java.lang.Boolean.TRUE).handler(
                new ChannelInitializer[SocketChannel] {
                    override def initChannel(ch: SocketChannel) = {
                        val pipe = ch.pipeline()
                        pipe.addLast("decoder", new StringDecoder())
                        pipe.addLast("encoder", new StringEncoder())
                        pipe.addLast(new TestClientHandler)
                    }
                }
            )
            val future = boot.connect(Host, Port).sync()
            future.channel().writeAndFlush(" hello my test!")
            future.channel.closeFuture().sync()
        } finally {
            group.shutdownGracefully()
        }
    }

}

